﻿using System;
using System.Globalization;
using Dynamic.Data.Model.DataType;

namespace Dynamic.Data.Model.DynamicEntities.Properties
{
    public sealed class PropertyFactory
    {
        private static PropertyFactory _instance = new PropertyFactory();
        private const string StatePropertyName = "StateCode";

        private PropertyFactory()
        {
        }

        public Property CreateInstance(string propertyName, object value)
        {
            Property property;
            if ((propertyName == null) || (propertyName.Length == 0))
            {
                throw new ArgumentException("Property name must be non null.", "propertyName");
            }
            if (value == null)
            {
                throw new ArgumentException("Property value must be non null.", "value");
            }
            bool ignoreCase = true;
            if (string.Compare(propertyName, "StateCode", ignoreCase, CultureInfo.InvariantCulture) == 0)
            {
                property = new StateProperty();
            }
            else if (value is string)
            {
                property = new StringProperty();
            }
            else if (value is UniqueIdentifier)
            {
                property = new UniqueIdentifierProperty();
            }
            else if (value is Status)
            {
                property = new StatusProperty();
            }
            else if (value is double)
            {
                property = new DoubleProperty();
            }
            //else if (value is Owner)
            //{
            //    property = new OwnerProperty();
            //}
            else if (value is Lookup)
            {
                property = new LookupProperty();
            }
            else if (value is Picklist)
            {
                property = new PicklistProperty();
            }
            else if (value is Key)
            {
                property = new KeyProperty();
            }
            else if (value is EntityNameReference)
            {
                property = new EntityNameReferenceProperty();
            }
            else if (value is DynamicEntity[])
            {
                property = new DynamicEntityArrayProperty();
            }
            else if (value is short)
            {
                property = new ShortProperty();
            }
            else if (value is int)
            {
                property = new IntProperty();
            }
            else if (value is long)
            {
                property = new LongProperty();
            }
            else if (value is Money)
            {
                property = new MoneyProperty();
            }
            else if (value is float)
            {
                property = new FloatProperty();
            }
            else if (value is Decimal)
            {
                property = new DecimalProperty();
            }
            else if (value is DateTime)
            {
                property = new DateTimeProperty();
            }
            else
            {
                if (!(value is bool))
                {
                    throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Value of type '{0}' is not supported.", new object[] { value.GetType().Name }), "value");
                }
                property = new BooleanProperty();
            }

            property.Name = propertyName;
            property.SetValue(value);
            return property;
        }

        public static PropertyFactory Instance
        {
            get
            {
                return _instance;
            }
        }
    }
}
